package com.zhaotianhao.controller;

import com.zhaotianhao.pojo.From;
import com.zhaotianhao.pojo.Users;
import com.zhaotianhao.service.GoodsService;
import com.zhaotianhao.service.UsersService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author Z-7
 * @Date 2022/2/23
 */
@Controller
@RequestMapping("goods")
public class GoodsController {
    @DubboReference
    private UsersService usersService;
    // 负载均衡共四种 随机权重 轮训(按顺序) 最小活跃 哈希相同
    @DubboReference(loadbalance = "random")// 采用轮训机制
    private GoodsService goodsService;
    @RequestMapping("to_login")
    public String to_login(){
        return "user_login";
    }
    @RequestMapping("login")
    public String login(Users users, Model model, HttpSession session){
        if(users == null){
            return "redirect:../goods/to_regirest";
        }
        Users login = usersService.login(users.getName(), users.getPwd());
        if(login == null){
            model.addAttribute("status","账号或密码错误");
            return "user_login";
        }
        session.setAttribute("LOGIN_USER",login);
        return "redirect:../goods/list";
    }
    @RequestMapping("to_regirest")
    public String to_regirest(){
        return "user_regirest";
    }
    @RequestMapping("regirest")
    public @ResponseBody boolean regirest(Users users){
        return usersService.insert(users) > 0;
    }
    @RequestMapping("list")
    public String to_list(@RequestParam(defaultValue = "1")int pageNum ,@RequestParam(defaultValue = "3")int pageSize,Model model){
        From from = goodsService.queryByPage(null, pageNum, pageSize);
        model.addAttribute("page",from.getPageInfo());
        model.addAttribute("from",from.getCome());
        return "goods_list";
    }
}
